#include "lib_fs_block_l0.h"
#include "lib_fs_block_l0.private"

#define BLOCK_SIZE      512
#define BLOCK_MALLOC    0x0
#define BLOCK_EOF       0xffffffff

uint32_t MediaBlockAlloc(OS_FILE_t *file)
{
    uint32_t i;
    uint32_t j;
    uint32_t ans;

    switch (file->fileEntry.mediaType)
    {
    case FILE_MEDIA_UNKNOWN:
        deadbeaf("MediaBlockAlloc, FILE_MEDIA_UNKNOWN");
        break;

    case FILE_MEDIA_RAM:
        deadbeaf("MediaBlockAlloc, FILE_MEDIA_RAM not supported");
        //ans = MediaBlockAlloc_ram(file->fileEntry.blockSize);
        break;

    case FILE_MEDIA_DISK:
        ans = MediaBlockAlloc_disk(file->fileEntry.blockSize);
        break;

    case FILE_MEDIA_FLASH:
        //Find empty flash block, and alloc it
        //code has been removed because it was too ugly
        deadbeaf("MediaBlockAlloc, FILE_MEDIA_FLASH has been removed");
        break;

    default:
        deadbeaf("MediaBlockAlloc, FILE_MEDIA_UNKNOWN2");
        break;
    }

    printf("MediaBlockAlloc, iblock=%lx ", ans);
    printf("\n");

    return ans;
}

void MediaBlockUnAlloc
(
    OS_FILE_t *file,
    uint32_t iblock
)
{
    if (file != NULL)
    {
        switch (file->fileEntry.mediaType)
        {
        case FILE_MEDIA_RAM:
            deadbeaf("MediaBlockAlloc, FILE_MEDIA_RAM not supported");
            //MediaBlockUnAlloc_ram(file, iblock);
            break;
        case FILE_MEDIA_DISK:
            MediaBlockUnAlloc_disk(file, iblock);
            break;
        case FILE_MEDIA_FLASH:
            deadbeaf("MediaBlockAlloc, FILE_MEDIA_FLASH not supported");
            break;
        default:
            break;
        }
    }
    else
    {
        deadbeaf("MediaBlockFree, file is NULL\n");
    }
}


void MediaBlockRead
(
    OS_FILE_t *file,
    uint32_t iblock
)
{
    switch (file->fileEntry.mediaType)
    {
    case FILE_MEDIA_RAM:
        deadbeaf("MediaBlockAlloc, FILE_MEDIA_RAM not supported");
        //MediaBlockRead_ram(file, iblock);
        break;
    case FILE_MEDIA_DISK:
        MediaBlockRead_disk(file, iblock);
        break;
    case FILE_MEDIA_FLASH:
        deadbeaf("MediaBlockAlloc, FILE_MEDIA_FLASH not supported");
        break;
    default:
        break;
    }
}


void MediaBlockWrite
(
    OS_FILE_t *file,
    uint32_t iblock
)
{
    switch (file->fileEntry.mediaType)
    {
    case FILE_MEDIA_RAM:
        deadbeaf("MediaBlockAlloc, FILE_MEDIA_RAM not supported");
        //MediaBlockWrite_ram(file, iblock);
        break;
    case FILE_MEDIA_DISK:
        MediaBlockWrite_disk(file, iblock);
        break;
    case FILE_MEDIA_FLASH:
        deadbeaf("MediaBlockAlloc, FILE_MEDIA_FLASH not supported");
        break;
    default:
        break;
    }
}

void MediaFilRelease
(
    OS_FILE_t *file
)
{
    my_free((void *) file);
}






